<?php
	function list_add() {
		global $kiwee;

		$name = addslashes(trim($_POST['name']));
		$desc = addslashes(trim($_POST['description']));
		
		if (strlen($name) < 1 || strlen($desc) < 1)
			die('ERROR: Please supply a name and a description');
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'lists (name, description) values ("' . $name . '","' . $desc . '")';
		mysql_query($qry);
	}		

	function list_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		
		$qry = 'select name from ' . $kiwee->config['fpx'] . 'options where lists_id=' . $id . ' order by name';
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) {
			mysql_query('delete from ' . $kiwee->config['fpx'] . 'lists_entries where lists_id=' . $id);
			mysql_query('delete from ' . $kiwee->config['fpx'] . 'lists where id=' . $id);
		} else {
			$lines = array();
			$lines[] = 'ERROR: List is already in use by these options: ';
			foreach ($rows as $row) {
				$lines[] = $row['name'] . ' ';
			}
			echo implode("\n", $lines);
		}
	}		

	function list_gridload() {
		global $kiwee, $_lang;
		
		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'lists order by name';
		$ds = mysql_query($sql) or die ('ERROR: ' . mysql_error());
		$grd = new DataGrid('',$ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='name, description';
		$grd->columns= $_lang['name'] . ', ' . $_lang['description'] . ',' . $_lang['kiwee_properties'] . ',' . $_lang['kiwee_entries'] . ',' . $_lang['delete'];
		$grd->colWidths='100,300,50,50,50';
		$grd->colAligns='left,left,center,center,center';
		$grd->colTypes=',,template:<button onClick="list_properties([+id+])">' . $_lang['kiwee_properties'] . '</button>,' .
					   'template:<button onClick="list_entries([+id+])">' . $_lang['kiwee_entries'] . '</button>,' .
					   'template:<button onClick="list_delete([+id+])">' . $_lang['delete'] . '</button>';
		echo $grd->render();
	}

	function list_properties() {
		global $kiwee;
		
		$id = intval($_POST['id']);

		$qry = 'select * from ' . $kiwee->config['fpx'] . 'lists where id=' . $id;
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) die('ERROR: ' . mysql_error());

		$lines = array();
		$lines[] = '<table border=0 cellpadding=5><tbody>';
		$lines[] = '<tr><td><label style="color: white;" for="uname">Name:</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="uname" name="uname" type="text" value="' . $rows[0]['name'] . '" length=20 /></td></tr>';
		$lines[] = '<tr><td><label style="color: white;" for="udesc">Description:</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="udesc" name="udesc" type="text" value="' . $rows[0]['description'] . '" length=20 /></td></tr>';
		$lines[] = '</tbody></table>';
		$lines[] = '<button onclick="list_update(' . $id . ')">Save</button>>';
		$lines[] = '<button onclick="hideBox()">Cancel</button>';
		echo implode("\n", $lines);
	}

	function list_update() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		$name = addslashes(trim($_POST['name']));
		$desc = addslashes(trim($_POST['description']));
		
		if (strlen($name) < 1 || strlen($desc) < 1)
			die('ERROR: Please supply a name and a description');
		
		$qry = 'update ' . $kiwee->config['fpx'] . 'lists set name="' . $name .	'",description="' . $desc . '" where id=' . $id;
		mysql_query($qry);
	}

	function list_entries() {
		global $kiwee, $_lang;
		
		$lines = array();
		
		$id = intval($_POST['id']);

		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'lists_options where lists_id=' . $id . ' order by value';
		$ds = mysql_query($sql) or die('ERROR: ' . mysql_error());
		$grd = new DataGrid('', $ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		//added by Bright @ 20080806 add a line for memo parts
		$grd->fields='name, value, priceoffset, memo';
		$grd->columns= $_lang['name'] . ', ' . $_lang['value'] . ',' . $_lang['kiwee_list_fld_offset'] . ',' . $_lang['kiwee_list_fld_memo'] . ',' . $_lang['kiwee_delete'];
		$grd->colWidths='120, 200, 80, 800, 20';
		$grd->colAligns='left,left,left,left,center';
		$grd->colTypes=',,,,template:<button style="width: auto" onClick="list_entries_delete([+id+])">X</button>';
		$lines[] = $grd->render();
		//edited by Bright @ 20080806 remove the <form> tag for ajax porpose
		//$lines[] = '<form><input type=text id="list_name" size=8>&nbsp;';
		$lines[] = '<input type=text id="list_name" size=8>&nbsp;';
		$lines[] = '<input type=text id="list_value" size=8>&nbsp;';
		$lines[] = '<input type=text id="list_offset" size=6>&nbsp;';
		//added by Bright @ 20080806 add a line for memo parts
		$lines[] = '<input type=textarea id="list_memo" >&nbsp;';
		$lines[] = '<button style="width: auto" onclick="list_entries_add(' . $id . ')">' . $_lang['add'] . '</button></form>';
		echo implode("\n", $lines);
	}

	function list_entries_add() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		$name = addslashes(trim($_POST['name']));
		$value = addslashes(trim($_POST['value']));
		$offset = floatval($_POST['offset']);
		$memo = addslashes(trim($_POST['memo']));	
		
		if (strlen($name) < 1 || strlen($value) < 1)
			die('ERROR: Please supply a name and a value');
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'lists_options (lists_id, name, value, priceoffset, memo) values (' . $id .
				',"' . $name . '","' . $value . '",' . $offset . ',"'. $memo .'")';
		mysql_query($qry);
		list_entries();
	}		

	function list_entries_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		$listid = intval($_POST['listid']);
		
		mysql_query('update ' . $kiwee->config['fpx'] . 'options set deflt=0 where deflt=' . $id . ' and lists_id=' . $listid);
		mysql_query('delete from ' . $kiwee->config['fpx'] . 'lists_options where id=' . $id);

		$_POST['id'] = $listid;
		list_entries();
	}		

	$oktxns[] = 'list_add';
	$oktxns[] = 'list_gridload';
	$oktxns[] = 'list_delete';
	$oktxns[] = 'list_properties';
	$oktxns[] = 'list_update';
	$oktxns[] = 'list_entries';
	$oktxns[] = 'list_entries_add';
	$oktxns[] = 'list_entries_delete';
?>